//
//  MusicViewController.swift
//  Reader
//
//  Created by mn on 2024/9/20.
//

import UIKit
import AVFoundation

class MusicViewController: BaseViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "音乐"
        loadFiles(type: .mp3)
        MusicManagerView.shared.files = files
        tableView.register(MusicListCell.self, forCellReuseIdentifier: "MusicListCell")
        tableView.separatorStyle = .singleLine
    }
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        UIView()
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        30
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "MusicListCell") as! MusicListCell
        cell.nameLabel.text = String(files[indexPath.row].name.dropLast(4))
        if let image = files[indexPath.row].coverImage{
            cell.corverImageView.image = image
        }
        if let durtionString = files[indexPath.row].durtionString{
            cell.durtionLabel.text = durtionString
        }
        return cell
    }
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        super.tableView(tableView, didSelectRowAt: indexPath)
        self.dismiss(animated: true)
    }
    override var currentFile: File?{
        didSet{
            MusicManagerView.shared.currentFile = currentFile
        }
    }
}
